<template>
  <el-card>
    <template #header> 商品列表 </template>
    <div>
      <!--  渲染表格数据 -->
      <el-table :data="goods" style="width: 100%">
        <el-table-column prop="name" label="商品名称"> </el-table-column
        ><el-table-column prop="category_name" label="分类名称">
        </el-table-column>
        <el-table-column prop="create_time" label="创建日期" width="180">
        </el-table-column>
        <el-table-column label="商品图片" width="150">
          <template v-slot="{ row }">
            <img :src="row.img_url" alt="" />
          </template>
        </el-table-column>
        <el-table-column prop="describe" label="商品描述"> </el-table-column>
        <el-table-column prop="price" label="商品价格"> </el-table-column>
        <el-table-column prop="sales_volume" label="销量"> </el-table-column>
        <el-table-column prop="evaluate_num" label="商品评价">
        </el-table-column>
        <el-table-column label="操作" width="180" fixed="right">
          <template v-slot="{ row }">
            <el-button
              type="primary"
              size="mini"
              @click="$router.push(`/goods/edit/${row.id}`)"
              >编辑</el-button
            >
            <el-button type="danger" size="mini" @click="handleDelete(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </el-card>
</template>

<script>
import { goodsList, delGoods } from '../../api/goods.js'
export default {
  data() {
    return {
      goods: [],
      currentPage: 1,
      pageSize: 5,
      total: 0
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList() {
      const users = JSON.parse(localStorage.getItem('users') || '{}')
      const { data, count } = await goodsList({
        member_id: users.member_id,
        access_token: users.access_token,
        page: this.currentPage,
        limit: this.pageSize
      })
      this.goods = data
      this.total = count
      console.log(data)
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.getGoodsList()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.currentPage = val
      this.getGoodsList()
    },
    handleDelete(id) {
      this.$confirm('是否删除该商品?', '提示', {
        cancelButtonText: '取消',
        confirmButtonText: '继续',

        type: 'warning'
      })
        .then(async () => {
          // 当用户点击确认删除的时候发出请求
          const users = JSON.parse(localStorage.getItem('users') || '{}')
          const { code, msg } = await delGoods({
            member_id: users.member_id,
            access_token: users.access_token,
            id: id
          })
          if (code === 1) {
            this.$message.success(msg)
            // console.log(data)
            this.getGoodsList()
          } else {
            this.$message.error(msg)
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style lang="less" scoped>
.el-card,
.el-table__body-wrapper {
  height: 100%;
  border: 0;
  overflow-x: hidden;
  img {
    width: 60px;
  }

  overflow-y: auto;
  &::-webkit-scrollbar {
    width: 6px;
    height: 6px;
  }
  &::-webkit-scrollbar-thumb {
    border-radius: 20px;
    background: #eee;
    cursor: pointer;

    &:hover {
      background: #bebebe;
    }

    &:active {
      background: #b0b0b0;
    }
    overflow-x: hidden;

    &::-webkit-scrollbar {
      display: none; // 重点
      -webkit-overflow-scrolling: touch;
    }
  }
}
</style>
